package com.oracle.lingshangejia.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lws.bugly.Bugly;
import com.oracle.lingshangejia.MyApp;
import com.oracle.lingshangejia.R;
import com.oracle.lingshangejia.base.BaseActivity;
import com.oracle.lingshangejia.utils.AppManagerDelegate;
import com.oracle.lingshangejia.utils.AppStateUtils;
import com.oracle.lingshangejia.utils.SpUtils;


public class SettingsActivity extends BaseActivity implements View.OnClickListener {

    private RelativeLayout re_back;
    private LinearLayout ll_change_pass;
    private LinearLayout ll_change_num;
    private TextView tv_tuichu;//退出登录
    private ImageView mIvMessage;//消息
    private boolean mBoolean;
    @Override
    public int intiLayout() {
        return R.layout.activity_settings;
    }

    @Override
    public void initView() {
        re_back=findViewById(R.id.re_back);
        re_back.setOnClickListener(this);
        ll_change_pass=findViewById(R.id.ll_change_pass);
        ll_change_pass.setOnClickListener(this);
        ll_change_num=findViewById(R.id.ll_change_num);
        ll_change_num.setOnClickListener(this);
        tv_tuichu=findViewById(R.id.tv_tuichu);
        TextView tvVersion = findViewById(R.id.tv_version);
        tvVersion.setText("版本："+AppStateUtils.getVerName(this ));
        tvVersion.setOnClickListener(this);
        tv_tuichu.setOnClickListener(this);
        mIvMessage = findViewById(R.id.iv_message);
        mIvMessage.setOnClickListener(this);
    }

    @Override
    public void initData() {

    }
    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.iv_message:
                if (mBoolean){
                    mIvMessage.setImageResource(R.drawable.jkt_me_push_off);
                    mBoolean = false;
                }else {
                    mIvMessage.setImageResource(R.drawable.jkt_me_push_on);
                    mBoolean = true;
                }
                break;

            //返回键
            case R.id.re_back:

                finish();
                break;

            //修改密码
            case R.id.ll_change_pass:
                startActivity(new Intent(this,ChangePassActivity.class));
                break;

            //修改手机号
            case R.id.ll_change_num:
                Intent intent=new Intent(this,ChangeNumActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_tuichu: //退出登录
                showNormalDialog();
                break;
            case R.id.tv_version:
                Bugly.checkUpgrade();
                break;
        }
    }

    /**
     * 是否退出对话框
     */
    private void showNormalDialog(){
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(SettingsActivity.this);
        normalDialog.setIcon(R.drawable.ic_launcher);
        normalDialog.setTitle("退出");
        normalDialog.setMessage("您是否要退出登录？");
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        MyApp.logout();
                        int mark= (int) SpUtils.get(SettingsActivity.this,"mark",0);
                        if(mark==1){  //社区医生退出

                          /*  SpUtils.put(SettingsActivity.this,"token","");
                            SpUtils.put(SettingsActivity.this,"ysid","");
                            SpUtils.put(SettingsActivity.this,"isLogin",false);
                            SpUtils.put(SettingsActivity.this,"mark",0);
                            SpUtils.put(SettingsActivity.this,"s_phone","");*/
                            SpUtils.clear(SettingsActivity.this);
                            AppManagerDelegate.getInstance().exitApp();
                        }else { //三甲医生退出

                          /*  SpUtils.put(SettingsActivity.this,"token","");
                            SpUtils.put(SettingsActivity.this,"isLogin",false);
                            SpUtils.put(SettingsActivity.this,"mark",0);
                            SpUtils.put(SettingsActivity.this,"userid","");
                            SpUtils.put(SettingsActivity.this,"sj_phone","");*/
                            SpUtils.clear(SettingsActivity.this);
                            setResult(2);
                            finish();
                        }
                        //...To-do
                    }
                });
        normalDialog.setNegativeButton("取消",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //...To-do
                        dialog.dismiss();
                    }
                });
        // 显示
        normalDialog.show();
    }
}
